package com.dimong.www.loan.net;

import android.text.TextUtils;

import com.dimong.loan.sdk.net.HttpUtil;
import com.dimong.loan.sdk.net.NetHttpCallback;
import com.dimong.loan.sdk.utils.DeviceUtils;
import com.dimong.loan.sdk.utils.MD5Utils;
import com.dimong.loan.sdk.utils.SharedUtil;
import com.dimong.loan.sdk.utils.Utils;
import com.dimong.www.loan.app.MyApplication;
import com.dimong.www.loan.app.ProductInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wuligang on 2018/5/9.
 */

public class LoadHttpUtil {
    public static void post(String url, Map<String, String> params, NetHttpCallback netHttpCallback) {
        HttpUtil.post(url, params, netHttpCallback);
    }

    public static String getSign(Map<String, String> params, String str) {
        String sign = "";
        List<String> list = new ArrayList<>();
        for (String key : params.keySet()) {
            list.add(key);
        }
        list.add(str);
        Collections.sort(list, new CaseInsensitiveComparator());
        for (int i = 0; i < list.size(); i++) {
            if (str.equals(list.get(i))) {
                sign = sign + list.get(i);
            } else {
                sign = sign + list.get(i);
                sign = sign + params.get(list.get(i));
            }
        }
        return MD5Utils.MD5(sign);
    }

    private static class CaseInsensitiveComparator
        implements Comparator<String>, java.io.Serializable {
        public int compare(String s1, String s2) {
            int n1 = s1.length();
            int n2 = s2.length();
            int min = Math.min(n1, n2);
            for (int i = 0; i < min; i++) {
                char c1 = s1.charAt(i);
                char c2 = s2.charAt(i);
//                if (isSupper(c1) && isLower(c2)) {
//                    return 1;
//                } else if (isSupper(c2) && isLower(c1)) {
//                    return -1;
//                } else {
                if (c1 != c2) {
                    return c1 - c2;
                }

//                }
            }
            return n1 - n2;
        }

        public boolean isLower(char ch) {
            if (ch >= 'a' && ch <= 'z') {
                return true;
            } else {
                return false;
            }
        }

        public boolean isSupper(char ch) {
            if (ch >= 'A' && ch <= 'Z') {
                return true;
            } else {
                return false;
            }
        }
    }

    public static void getIndexData(NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("product_id", ProductInfo.getInstance().getProductId());
        params.put("channel_id", ProductInfo.getInstance().getChannelId());
        params.put("classify_num", "");
        params.put("sign", getSign(params, "sProd.getIndexData"));
        post(UrlConstants.GET_INDEX_DATA, params, netHttpCallback);
    }

    public static void getProdList(int page, String classifyId, NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("product_id", ProductInfo.getInstance().getProductId());
        params.put("page", page + "");
        params.put("classify_id", classifyId);
        params.put("pagesize", "");
        params.put("attr_id", "");
        params.put("sign", getSign(params, "sProd.getProdList"));
        post(UrlConstants.GET_PROD_LIST, params, netHttpCallback);
    }

    public static void getProdDetailed(String prod_id, NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("prod_id", prod_id);
        params.put("sign", getSign(params, "sProd.getProdDetailed"));
        post(UrlConstants.GET_PROD_DETAILED, params, netHttpCallback);
    }

    public static void getCardList(int page, NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page + "");
        params.put("pagesize", "");
        params.put("sign", getSign(params, "sProd.getCardList"));
        post(UrlConstants.GET_CARD_LIST, params, netHttpCallback);
    }

    public static void sendSms(String phone, NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("sms_type", "1");
        params.put("phone", phone);
        params.put("device_id", DeviceUtils.getDeviceId(MyApplication.getAppContext()));
        params.put("ip", DeviceUtils.getIPAddress(MyApplication.getAppContext()));
        params.put("product_id", ProductInfo.getInstance().getProductId());
        params.put("channel_id", ProductInfo.getInstance().getChannelId());
        params.put("sign", getSign(params, "sSms.sendSms"));
        post(UrlConstants.SEND_SMS, params, netHttpCallback);
    }

    public static void checkUserLogin(String phone, String code, NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone);
        params.put("code", code);
        params.put("product_id", ProductInfo.getInstance().getProductId());
        params.put("channel_id", ProductInfo.getInstance().getChannelId());
        params.put("sign", getSign(params, "sUser.checkUserLogin"));
        post(UrlConstants.CHECK_USER_LOGIN, params, netHttpCallback);
    }

    public static void getUserIndexPage(String phone, NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone);
        params.put("product_id", ProductInfo.getInstance().getProductId());
        params.put("channel_id", ProductInfo.getInstance().getChannelId());
        params.put("version", Utils.getAppVersionCode(MyApplication.getAppContext()) + "");
        params.put("sign", getSign(params, "sUser.getUserIndexPage"));
        post(UrlConstants.GET_USER_INDEX_PAGE, params, netHttpCallback);
    }

    public static void upgrade(NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("product_id", ProductInfo.getInstance().getProductId());
        params.put("channel_id", ProductInfo.getInstance().getChannelId());
        params.put("version", Utils.getAppVersionCode(MyApplication.getAppContext()) + "");
        params.put("sign", getSign(params, "sProduct.upgrade"));
        post(UrlConstants.UPGRADE, params, netHttpCallback);

    }

    public static void subLoanLog(String prod_type, String prod_id, String money, String term, NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        String phone = SharedUtil.getPreferStr("phone");
        if (TextUtils.isEmpty(phone)) {
            phone = "";
        }
        params.put("prod_type", prod_type);
        params.put("phone", phone);
        params.put("prod_id", prod_id);
        params.put("money", money);
        params.put("term", term);
        params.put("product_id", ProductInfo.getInstance().getProductId());
        params.put("channel_id", ProductInfo.getInstance().getChannelId());
        params.put("sign", getSign(params, "sUser.subLoanLog"));
        post(UrlConstants.SUB_LOAN_LOG, params, netHttpCallback);
    }

    public static void clickCard(String card_id, NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("card_id", card_id);
        params.put("sign", getSign(params, "sUser.clickCard"));
        post(UrlConstants.CLICK_CARD, params, netHttpCallback);
    }

    public static void getUserProtocolUrl(NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("product_id", ProductInfo.getInstance().getProductId());
        params.put("sign", getSign(params, "sProduct.getUserProtocolUrl"));
        post(UrlConstants.GET_USER_PRODUCT_URL, params, netHttpCallback);
    }

    public static void getCmsList(int page, NetHttpCallback netHttpCallback) {
        Map<String, String> params = new HashMap<>();
        params.put("page", page + "");
        params.put("pagesize", "");
        params.put("sign", getSign(params, "sCms.getList"));
        post(UrlConstants.GET_CMS_LIST, params, netHttpCallback);
    }

}
